package com.epoint.bookmanagement.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.mysql.jdbc.PreparedStatement;

/**
 * jdbc jdbc工具类
 * @author 
 *
 */
public class JDBCUtil {
	
	/**
	 * jdbc连接(使用druid连接池)
	 * @return Connection 连接对象
	 */

	public static Connection getConnection() {
		DruidDataSource druidDataSouce = DSUtil.getDruidDataSouce();
		Connection con = null;
		try {
			con = druidDataSouce.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return con;
	}
	
	/**
	 * 关闭连接
	 * @param resultSet
	 * @param statement
	 * @param connection
	 */

	public static void closeConnection(ResultSet resultSet, Statement st, Connection con) {
		try {
			if (resultSet != null) {
				resultSet.close();
			}
			if (st != null) {
				st.close();		
					}
			if (con != null) {
				con.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 重载关闭连接方法
	 * @param resultSet
	 * @param prepare
	 * @param connection
	 */
	public static void closeConnection(ResultSet resultSet, PreparedStatement ps, Connection con) {
		try {
			if (resultSet != null) {
				resultSet.close();
			}
			if (ps != null) {
				ps.close();		
					}
			if (con != null) {
				con.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
